package com.zx.sdk.league.member;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.beizi.fusion.AdListener;
import com.beizi.fusion.BeiZis;
import com.beizi.fusion.RewardedVideoAd;
import com.beizi.fusion.RewardedVideoAdListener;
import com.beizi.fusion.SplashAd;
import com.zx.sdk.ZxSDK;
import com.zx.sdk.listener.BaseZxRewardListener;
import com.zx.sdk.listener.BaseZxSplashListener;
import com.zx.sdk.model.AdInfo;
import com.zx.sdk.model.ReadyAdPosition;
import com.zx.sdk.model.ZxError;
import com.zx.sdk.util.LogHelper;
import com.zx.sdk.util.RunnableHelper;
import com.zx.sdk.util.UiHelper;

import java.util.HashMap;

public class BeiZi extends BaseLeagueMember<SplashAd, RewardedVideoAd, Object, Object, Object> {
    @Override
    public String getName() {
        return "beizi";
    }

    @Override
    protected void onInit(Context context, String appId, boolean initialized) {
        BeiZis.init(context, appId, null, null, ZxSDK.oaid);
    }

    @Override
    protected void onLoadSplash(AdInfo adInfo, ViewGroup container, String mode, String type, BaseZxSplashListener listener) {
        SplashAd[] splashAd = new SplashAd[1];
        splashAd[0] = new SplashAd(container.getContext(), null, adInfo.getMapPid(), new AdListener() {

            @Override
            public void onAdLoaded() {
                saveSplash(adInfo.getMapPid(), splashAd[0], container);
                if (ZxSDK.FETCH_AND_SHOW.equals(mode)) {
                    if (!showSplash(adInfo, container, listener)) {
                        listener.onNoAD(BeiZi.this, new ZxError("-1", "广告显示失败"), adInfo);
                        return;
                    }
                }
                listener.onADLoaded(BeiZi.this, 0, adInfo);
            }

            @Override
            public void onAdShown() {
                listener.onADExposure(BeiZi.this, adInfo);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        listener.onPreLoadNoAD(BeiZi.this, new ZxError(String.valueOf(errorCode), errorCode + ""), adInfo);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        listener.onNoAD(BeiZi.this, new ZxError(String.valueOf(errorCode), errorCode + ""), adInfo);
                        break;
                }
            }

            @Override
            public void onAdClosed() {
                listener.onADDismissed(BeiZi.this, adInfo);
            }

            @Override
            public void onAdTick(long millisUnitFinished) {
                listener.onADTick(BeiZi.this, millisUnitFinished, adInfo);
            }

            @Override
            public void onAdClicked() {
                listener.onADClicked(BeiZi.this, adInfo);
            }
        }, 5000);//广告请求超时时长，建议5秒以上,该参数单位为ms
        RunnableHelper.runOnUIThread(() -> {
            splashAd[0].loadAd(UiHelper.px2Dp(UiHelper.getScreenWidth()), UiHelper.px2Dp(UiHelper.getScreenHeight()));//第一个参数是宽度，第二个参数是高度
        });
    }

    @Override
    public boolean onShowSplash(AdInfo adInfo, ViewGroup container, @Nullable SplashAd splash, BaseZxSplashListener listener) {
        if (splash == null) return false;
        RunnableHelper.runOnUIThread(() -> {
            splash.show(container);
        });
        return true;
    }

    @Override
    public void onLoadReward(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {
        RewardedVideoAd[] mRewardedVideoAd = new RewardedVideoAd[1];
        mRewardedVideoAd[0] = new RewardedVideoAd(context, adInfo.getMapPid(), new RewardedVideoAdListener() {

            @Override
            public void onRewarded() {
                listener.onReward(BeiZi.this, new HashMap<>(), adInfo);
            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        listener.onPreLoadADError(BeiZi.this, new ZxError(String.valueOf(i), i + ""), adInfo);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        listener.onNoAD(BeiZi.this, new ZxError(String.valueOf(i), i + ""), adInfo);
                        break;
                }
            }

            @Override
            public void onRewardedVideoAdLoaded() {
                switch (mode) {
                    case ZxSDK.FETCH_ONLY:
                        saveReward(adInfo.getMapPid(), mRewardedVideoAd[0]);
                        break;
                    case ZxSDK.FETCH_AND_SHOW:
                        if (mRewardedVideoAd[0] != null && mRewardedVideoAd[0].isLoaded()) {
                            mRewardedVideoAd[0].showAd(context);
                        }
                        break;
                }
                listener.onADLoad(BeiZi.this, adInfo);
            }

            @Override
            public void onRewardedVideoAdShown() {
                listener.onADExpose(BeiZi.this, adInfo);
            }

            @Override
            public void onRewardedVideoAdClosed() {
                listener.onADClose(BeiZi.this, adInfo);
            }

            @Override
            public void onRewardedVideoClick() {
                listener.onADClick(BeiZi.this, adInfo);
            }

            @Override
            public void onRewardedVideoComplete() {

            }

        }, 10000, 1);//加载激励视频的超时时长，建议10秒以上,该参数单位为ms；第五个参数是：模板类型（1表示平台模板为1.0；2表示平台模板2.0）
        RunnableHelper.runOnUIThread(() -> {
            mRewardedVideoAd[0].loadAd();
        });
    }

    @Override
    public boolean onShowReward(Activity context, AdInfo adInfo, @Nullable RewardedVideoAd reward, BaseZxRewardListener listener) {
        if (reward != null && reward.isLoaded()) {
            RunnableHelper.runOnUIThread(() -> {
                reward.showAd(context);
            });
            return true;
        }
        return false;
    }

    @Override
    public void onLoadInterstitial(Activity context, AdInfo adInfo, String mode, String type, BaseZxRewardListener listener) {

    }

    @Override
    public boolean onShowInterstitial(Activity context, AdInfo adInfo, @Nullable Object interstitial, BaseZxRewardListener listener) {
        return false;
    }

    @Override
    public int getCpmByPid(String type, String pid) {
        int cpm = super.getCpmByPid(type, pid);
        try {
            switch (type) {
                case ZxSDK.SPLASH:
                    if (splashMap.containsKey(pid)) {
                        cpm = splashMap.get(pid).getECPM();
                    }
                case ZxSDK.REWARD:
                    if (rewardMap.containsKey(pid)) {
                        cpm = rewardMap.get(pid).getECPM();
                    }
            }
        } catch (Exception exception) {
            LogHelper.d("beizi getCpmByPid exception " + exception.getMessage());
        }
        cpmCache.put(type + pid, cpm);
        return cpm;
    }

    @Override
    public boolean isPreResReady(String type, String pid) {
        switch (type) {
            case ZxSDK.SPLASH:
                return splashMap.get(pid) != null;
            case ZxSDK.REWARD:
                RewardedVideoAd rewardVideoAD = getFilledRewardByPid(pid);
                return rewardVideoAD != null && rewardVideoAD.isLoaded();
        }
        return false;
    }

    @Override
    public void destroySplash(Activity activity) {
        for (String key : splashMap.keySet()) {
            SplashAd splashAd = splashMap.get(key);
            if (splashAd != null) {
                splashAd.reportNotShow();
                splashAd.cancel(activity);
            }
        }
    }

    @Override
    public void destroyShownSplash(Activity activity, SplashAd splashAd) {
        if (splashAd != null) {
            splashAd.cancel(activity);
        }
    }

    @Override
    public void notifyBiddingLose(String type, String mapPid, ReadyAdPosition winner, ZxSDK.LoseReason reason) throws Exception {
        super.notifyBiddingLose(type, mapPid, winner, reason);
    }

    @Override
    public void destroyReward(Activity activity) {
        for (String key : rewardMap.keySet()) {
            RewardedVideoAd rewardedVideoAd = rewardMap.get(key);
            if (rewardedVideoAd != null) {
                rewardedVideoAd.destroy();
            }
        }
    }

    @Override
    public void destroyShownReward(Activity activity, RewardedVideoAd rewardAd) {
        if (rewardAd != null) {
            rewardAd.destroy();
        }
    }
}
